Micron Document
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| SparkN0de-git | SparkN0de |
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


Commit 1a7d43c5de3e11acfeb10460726466622d2e4b0e


Parents : dff0fd5
Author : Mark Qvist <mark@unsigned.io>
Date : 2025-11-24T01:45:27+01:00

Load pre-compiled filterlib if available

Changes

2 files changed, 33 insertions(+), 11 deletions(-)

M LXST/Filters.py +20 -7
M setup.py +13 -4

Diff

diff --git a/LXST/Filters.py b/LXST/Filters.py
index 4124361..af8d0fd 100644
--- a/LXST/Filters.py
+++ b/LXST/Filters.py
@@ -4,21 +4,34 @@ import time
import RNS
import os
+USE_NATIVE_FILTERS = False
if not find_spec("cffi"):
- USE_NATIVE_FILTERS = False
RNS.log(f"Could not load CFFI module for filter acceleration, falling back to Python filters. This will be slow.", RNS.LOG_WARNING)
RNS.log(f"Make sure that the CFFI module is installed and available.", RNS.LOG_WARNING)
else:
try:
- # TODO: Load pre-compiled so/dll
from cffi import FFI
import pathlib
- ffi = FFI()
c_src_path = pathlib.Path(__file__).parent.resolve()
- with open(os.path.join(c_src_path, "Filters.h"), "r") as f: ffi.cdef(f.read())
- with open(os.path.join(c_src_path, "Filters.c"), "r") as f: c_src = f.read()
- native_functions = ffi.verify(c_src)
- USE_NATIVE_FILTERS = True
+ ffi = FFI()
+
+ try:
+ filterlib_spec = find_spec("LXST.filterlib")
+ if not filterlib_spec or filterlib_spec.origin == None: raise ImportError("Could not locate pre-compiled LXST.filterlib module")
+ with open(os.path.join(c_src_path, "Filters.h"), "r") as f: ffi.cdef(f.read())
+ native_functions = ffi.dlopen(filterlib_spec.origin)
+ USE_NATIVE_FILTERS = True
+
+ except Exception as e:
+ RNS.log(f"Could not load pre-compiled LXST filters library. The contained exception was: {e}", RNS.LOG_WARNING)
+ RNS.log(f"Attempting to compile library from source...", RNS.LOG_WARNING)
+
+ if USE_NATIVE_FILTERS == False:
+ with open(os.path.join(c_src_path, "Filters.h"), "r") as f: ffi.cdef(f.read())
+ with open(os.path.join(c_src_path, "Filters.c"), "r") as f: c_src = f.read()
+ native_functions = ffi.verify(c_src)
+ USE_NATIVE_FILTERS = True
+
except Exception as e:
RNS.log(f"Could not compile modules for filter acceleration, falling back to Python filters. This will be slow.", RNS.LOG_WARNING)
RNS.log(f"The contained exception was: {e}", RNS.LOG_WARNING)

diff --git a/setup.py b/setup.py
index ee93810..b67817e 100644
--- a/setup.py
+++ b/setup.py
@@ -1,10 +1,16 @@
import setuptools
+from setuptools import setup, Extension
+from setuptools.command.build_ext import build_ext
+import os
+import platform
-with open("README.md", "r") as fh:
- long_description = fh.read()
-
+with open("README.md", "r") as fh: long_description = fh.read()
exec(open("LXST/_version.py", "r").read())
+extensions = [
+ Extension("LXST.filterlib", sources=["LXST/Filters.c"], include_dirs=["LXST"], language="c"),
+]
+
packages = setuptools.find_packages(exclude=[])
packages.append("LXST.Utilities")
packages.append("LXST.Primitives.hardware")
@@ -34,6 +40,8 @@ setuptools.setup(
url="https://git.unsigned.io/markqvist/lxst",
packages=packages,
package_data=package_data,
+ ext_modules=extensions,
+ cmdclass={"build_ext": build_ext},
classifiers=[
"Programming Language :: Python :: 3",
"License :: Other/Proprietary License",
@@ -49,6 +57,7 @@ setuptools.setup(
"soundcard>=0.4.5",
"numpy>=2.3.4",
"pycodec2>=4.1.0",
- "audioop-lts>=0.2.1;python_version>='3.13'"],
+ "audioop-lts>=0.2.1;python_version>='3.13'",
+ "cffi"],
python_requires=">=3.7",
)


──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────